home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 412_01 / src / list / list.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-31  |  2.6 KB  |  173 lines

  1. #include "list.h"
  2.  
  3. LIST_::LIST_(int dstr)
  4. {
  5.     head = tail = found = NULL;
  6.     nodecount = 0;
  7.     deallocondestr = dstr;
  8. }
  9.  
  10.  
  11. LIST_::~LIST_()
  12. {
  13.     LIST_NODE_
  14.         *p;
  15.  
  16.     LIST_NODE_::destroy = (deallocondestr == DEALLOC_ON_DESTRUCT) ? 
  17.                           DEALLOC : NODEALLOC;
  18.  
  19.     while(head)
  20.     {
  21.     p = head;
  22.         head = head->getnext();
  23.         delete(p);
  24.     }
  25. }
  26.  
  27.  
  28. VOBJECT_ *LIST_::gethead() const
  29. {
  30.     return(head ? head->getdata() : NULL);
  31. }
  32.  
  33.  
  34. VOBJECT_ *LIST_::gettail() const
  35. {
  36.     return(tail ? tail->getdata() : NULL);
  37. }
  38.  
  39.  
  40. int LIST_::getcount() const
  41. {
  42.     return(nodecount);
  43. }
  44.  
  45.  
  46. int LIST_::getdestruct() const
  47. {
  48.     return(deallocondestr);
  49. }
  50.  
  51.  
  52. void LIST_::setdestruct(int dstr)
  53. {
  54.     deallocondestr = dstr;
  55. }
  56.  
  57.  
  58. void LIST_::addtohead(VOBJECT_ &obj)
  59. {
  60.     nodecount++;
  61.     if (!head)
  62.     {
  63.         head = new LIST_NODE_(obj);
  64.         tail = head;
  65.     }
  66.     else
  67.     {
  68.         head = new LIST_NODE_(obj, head, NULL);
  69.         head->getnext()->setprev(head);
  70.     }
  71. }
  72.  
  73.  
  74. void LIST_::addtotail(VOBJECT_ &obj)
  75. {
  76.     if (!tail)
  77.         addtohead(obj);
  78.     else
  79.     {
  80.     nodecount++;
  81.         tail->setnext(new LIST_NODE_(obj, NULL, tail));
  82.         tail = tail->getnext();
  83.     }
  84. }
  85.  
  86.  
  87. VOBJECT_ *LIST_::lookup(VOBJECT_ &find)
  88. {
  89.     LIST_NODE_
  90.         *p;
  91.  
  92.     for (p = head; p; p = p->getnext())
  93.     {
  94.         if (find.equal(*p->getdata()))
  95.         {
  96.             found = p;
  97.             return(p->getdata());
  98.         }
  99.     }
  100.  
  101.     return(NULL);
  102. }
  103.  
  104.  
  105. void LIST_::remove_head(int dstr)
  106. {
  107.     remove_node(head, dstr);
  108. }
  109.  
  110.  
  111. void LIST_::remove_tail(int dstr)
  112. {
  113.     remove_node(tail, dstr);
  114. }
  115.  
  116.  
  117. void LIST_::remove_found(int dstr)
  118. {
  119.     remove_node(found, dstr);
  120. }
  121.  
  122.  
  123. void LIST_::remove_node(LIST_NODE_ *node, int dstr)
  124. {
  125.     if (!node)
  126.     return;
  127.  
  128.     nodecount--;
  129.     LIST_NODE_::destroy = dstr;
  130.  
  131.     if (node == found)
  132.     found = NULL;
  133.  
  134.     if (node == head)
  135.     {
  136.     if ((head = head->getnext()) != NULL)
  137.         head->setprev(NULL);
  138.     if (tail == node)
  139.         tail = head;
  140.     }
  141.     else if (node == tail)
  142.     {
  143.     tail = tail->getprev();
  144.     tail->setnext(NULL);
  145.     }
  146.     else
  147.     {
  148.         node->getprev()->setnext(node->getnext());
  149.         node->getnext()->setprev(node->getprev());
  150.     }
  151.    
  152.     delete(node);
  153. }
  154.  
  155.  
  156. void LIST_::clear(int dstr)
  157. {
  158.     LIST_NODE_
  159.         *p;
  160.  
  161.     LIST_NODE_::destroy = dstr;
  162.                      
  163.     while(head)
  164.     {
  165.     p = head;
  166.         head = head->getnext();
  167.         delete(p);
  168.     }
  169.     head = tail = found = NULL;
  170.     nodecount = 0;
  171. }
  172.  
  173.